package avicit.bdp.dds.server.worker.task.sqoop.generator.targets;

import avicit.bdp.common.datasource.BaseDataSource;
import avicit.bdp.common.datasource.DataSourceFactory;
import avicit.bdp.common.utils.DESUtils;
import avicit.bdp.dds.dispatch.task.sqoop.SqoopParameters;
import avicit.bdp.dds.dispatch.task.sqoop.targets.TargetMysqlParameter;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dds.server.entity.SqoopTaskExecutionContext;
import avicit.bdp.dds.server.entity.TaskExecutionContext;
import avicit.bdp.dds.server.worker.task.sqoop.generator.ITargetGenerator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * mysql target generator
 */
public class MysqlTargetGenerator implements ITargetGenerator {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public String generate(SqoopParameters sqoopParameters, TaskExecutionContext taskExecutionContext) {

        StringBuilder result = new StringBuilder();
        try{

            TargetMysqlParameter targetMysqlParameter =
                    JSONUtils.parseObject(sqoopParameters.getTargetParams(),TargetMysqlParameter.class);

            SqoopTaskExecutionContext sqoopTaskExecutionContext = taskExecutionContext.getSqoopTaskExecutionContext();

            if(targetMysqlParameter != null && StringUtils.isNotBlank(targetMysqlParameter.getTargetDatasource())){

                // get datasource
                BaseDataSource baseDataSource = DataSourceFactory.getDatasource(sqoopTaskExecutionContext.getTargetType(),
                        sqoopTaskExecutionContext.getTargetConnectionParams());

                if(baseDataSource != null){
                    result.append(" --connect ")
                            .append(baseDataSource.getJdbcUrl())
                            .append(" --username ")
                            .append(baseDataSource.getUser())
                            .append(" --password ")
                            .append(DESUtils.decrypt(baseDataSource.getPassword()))
                            .append(" --table ")
                            .append(targetMysqlParameter.getTargetTable());

                    if(StringUtils.isNotEmpty(targetMysqlParameter.getTargetColumns())){
                        result.append(" --columns ").append(targetMysqlParameter.getTargetColumns());
                    }

                    if(StringUtils.isNotEmpty(targetMysqlParameter.getFieldsTerminated())){
                        result.append(" --fields-terminated-by '").append(targetMysqlParameter.getFieldsTerminated()).append("'");
                    }

                    if(StringUtils.isNotEmpty(targetMysqlParameter.getLinesTerminated())){
                        result.append(" --lines-terminated-by '").append(targetMysqlParameter.getLinesTerminated()).append("'");
                    }

                    if(targetMysqlParameter.isUpdate()
                            && StringUtils.isNotEmpty(targetMysqlParameter.getTargetUpdateKey())
                            && StringUtils.isNotEmpty(targetMysqlParameter.getTargetUpdateMode())){
                        result.append(" --update-key ").append(targetMysqlParameter.getTargetUpdateKey())
                              .append(" --update-mode ").append(targetMysqlParameter.getTargetUpdateMode());
                    }
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }

        return result.toString();
    }
}
